<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class SB_Controller extends CI_Controller {

	public function __construct($admin = false)
	{
		date_default_timezone_set("Asia/Yekaterinburg");
		parent::__construct($admin);
	}

	public function getPage($data = array())
	{
		if (!is_array($data))
			$data = array();
		
		if (isset($data['adminPage']))
			$this->load->view('admin/index', $data);
			else
		{
			$data['subMenu'] = array();
			if (empty($data['addCSS']))
				$data['addCSS'] = array();

			if (empty($data['content']))
			{
				if ($vContent = $this->main_model->getContentByUrl())
				{
					if (empty($data['title'])) {
						$data['title'] = $vContent['title'].' - '.$this->settings->getTitle();
					}
					$data = array_merge($vContent, $data);

					if (!empty($vContent['addStyles'])) {
						foreach ($vContent['addStyles'] as $val) {
							if ($val->pType)
								$data['addJS'][] = $val->pUrl;
							else
								$data['addCSS'][] = $val->pUrl;
						}
					}
				}

				if (!empty($vContent['id'])) {
					$data['subMenu'] = $this->main_model->getSubMenu($vContent['id']);
				}
			}

			if (empty($data['title'])) {
				$data['title'] = $this->settings->getTitle();
			}

			$data['mainMenu'] = $this->main_model->getMenuPages();
			$data['rightContent'] = $this->main_model->getRightBlock();
			$data['headContent'] = $this->main_model->getBlock(2);
			$data['bottomContent'] = $this->main_model->getBlock(3);

			$data['activePage'] = $this->uri->segment(1);
			if (!$data['activePage'] && isset($this->main_model->currentPage->pUrl)) {
				$data['activePage'] = $this->main_model->currentPage->pUrl;
			}

			if (isset($data['addJS']) && is_array($data['addJS'])) {
				$data['addJS'] = array_unique($data['addJS']);
			}
			if (isset($data['addCSS']) && is_array($data['addCSS'])) {
				$data['addCSS'] = array_unique($data['addCSS']);
			}
			echo $this->load->view('index', $data, true);
			die();
		}
	}
}

/**
* Class for admin pages
*/
class Admin_Controller extends SB_Controller
{
	public function __construct()
	{
		parent::__construct(true);
		
		if ($rights = func_get_args())
		{
			$access = false;
			foreach ($rights as $right) {
				if ($right == 'admin')
					$access |= $this->user->isAdmin();
				else
					$access |= $this->user->checkRight($right);
			}

			if (!$access)
				redirect('/admin');
		}

		$this->load->model('admin');
	}

	public function getPage($data = array())
	{
		$data['adminPage'] = true;
		parent::getPage($data);
	}
}